package edu.nyu.hps.architecture;

import java.awt.Color;
import java.awt.Font;
import java.awt.Graphics;
import java.awt.Image;
import java.awt.image.BufferedImage;
import java.io.IOException;
import java.net.MalformedURLException;
import java.net.URL;

import javax.imageio.ImageIO;
import javax.swing.ImageIcon;
import javax.swing.JLabel;
import javax.swing.JPanel;

import sun.java2d.loops.DrawLine;

public class DrawingPanel extends JPanel {
	private static final long serialVersionUID = 1L;

	public static int GREEN_FIELD = 0;
	public static int CAT_TRAVELLED_FIELD = 1;
	public static int STOP_AT_FIELD = 2;
	public static int BASIN_FIELD = 3;
	public static int CAT_FIELD = 4;
	public static int MOUSE_FIELD = 5;
	public static int MOUSE_TRAVELLED_FIELD = 6;

	public int SIZE_SQUARE_BASIN = 7;

	int[][] field = new int[100][100];
	int[][] prevfield = new int[100][100];
	int turn = 0;

	int lastMouseX, lastMouseY;
	int lastCatX, lastCatY;

	int mx = 0	, my = 0;
	int cx = 0, cy = 0;

	String catname = "Tom", mousename = "Jerry";

	boolean hunt = true;
	Graphics g, gtemp;  

	public DrawingPanel() {
		// setBounds(0, 0, 700, 700);
		// setLocation(0, 0);
		for (int i = 50; i <= 65; i++) {
			for (int j = 50; j <= 65; j++) {
				field[i][j] = BASIN_FIELD;
			}
		}
		setOpaque(true);

//		setDoubleBuffered(true);
		// setBackground(Color.white);

	}

	private Color getColor(int code) {
		if (CAT_TRAVELLED_FIELD == code) {
			return Color.yellow;
		}else if(MOUSE_TRAVELLED_FIELD == code){
			return Color.cyan;
		}else if (STOP_AT_FIELD == code) {
			return Color.red;
		} else if (BASIN_FIELD == code) {
			return Color.black;
		} else if (MOUSE_FIELD == code) {
			return Color.blue;
		} else if (CAT_FIELD == code) {
			return Color.red;
		} else if (GREEN_FIELD == code) {
			return new Color(102, 204, 51);
		} else {
			return Color.green;
		}
	}

	public void setPoint(int x, int y, int type) {
		for (int i = 0; i < 100; i++) {
			for (int j = 0; j < 100; j++) {
				prevfield[i][j] = field[i][j];
			}
		}
		if (type == CAT_FIELD) {
			for (int i = 0; i < 100; i++) {
				for (int j = 0; j < 100; j++) {
					if (field[i][j] == CAT_FIELD) {
						field[i][j] = CAT_TRAVELLED_FIELD;
					}
				}
			}
			cx = x;
			cy = y;
		}
		if (type == MOUSE_FIELD) {
			for (int i = 0; i < 100; i++) {
				for (int j = 0; j < 100; j++) {
					if (field[i][j] == MOUSE_FIELD) {
						field[i][j] = MOUSE_TRAVELLED_FIELD;
					}
				}
			}
			mx = x;
			my = y;
		}
		field[x][y] = type;
	}

	@Override
	public void paintComponent(Graphics g) {

		// super.paintComponent(g);
		g.setColor(getColor(GREEN_FIELD));
		g.fillRect(0, 0, 700, 700);
//		System.out.println("Inside Drawing Panel paintComponent");

		for (int i = 0; i < field.length; i++) {
			for (int j = 0; j < field[0].length; j++) {
				if (field[i][j] != GREEN_FIELD
						|| field[i][j] != prevfield[i][j]) {
					g.setColor(getColor(field[i][j]));
					g.fillRect(i * SIZE_SQUARE_BASIN, j * SIZE_SQUARE_BASIN,
							SIZE_SQUARE_BASIN, SIZE_SQUARE_BASIN);
				}
			}
		}

		// filling the status area
		g.setColor(Color.white);
		g.fillRect(700, 0, 300, 700);

		// Draw lines
			drawLines(g);
			
		// Drawing the basin string
		drawBasin(g);

		// draw the turn information
		drawTurn(g);

		// draw hunt status
		drawHunt(g);

		// draw the cat information
		drawCat(g);

		// draw the mouse information
		drawMouse(g);

	}

	private void drawHunt(Graphics g) {

	}

	private void drawLines(Graphics g) {
		g.setColor(Color.black);
		for (int i = 0; i < 100; i++) {
			g.drawLine(i * 7, 1, i * 7, 700);
			g.drawLine(1, i * 7, 700, i * 7);
		}

		g.drawLine(750, 233, 950, 233);
		g.drawLine(750, 433, 950, 433);
	}

	private void drawMouse(Graphics g) {
		String mouse = "MOUSE: " + mousename;
		g.setColor(Color.blue);
		g.drawString(mouse, 800, 500);

		String cpos = "< " + mx + " : " + my + " >";
		g.drawString(cpos, 800, 550);
	}

	private void drawCat(Graphics g) {
		// JLabel label = new JLabel(new ImageIcon(tom));
		String cat = "CAT: " + catname;
		g.setColor(Color.red);
		g.drawString(cat, 800, 300);

		String cpos = "< " + cx + " : " + cy + " >";
		g.drawString(cpos, 800, 350);

	}

	private void drawTurn(Graphics g) {
		Font f = new Font("Comic Sans", Font.BOLD, 24);
		g.setFont(f);
		g.setColor(Color.black);
		g.drawString("TURN  : " + turn, 800, 50);
		String h = "STATUS: ";
		h += (hunt == true) ? "PLAYING!" : "CAUGHT!";
		g.drawString(h, 750, 150);
	}

	private void drawBasin(Graphics g) {
		g.setColor(Color.WHITE);
		Font f = new Font("Comic Sans", Font.BOLD, 12);
		g.setFont(f);
		g.drawString("BASIN", 55 * 7, 55 * 7);
	}

	public void update(Graphics g) {
		paintImmediately(0,0,1000,700);
	}

	public void setHunt(boolean hunt) {
		this.hunt = hunt;
	}

	public void increaseTurn() {
		turn++;
	}
	
	public void setTurn(int t){
		turn = t;
	}

	public void resetfield(){
		for(int i=0;i<field.length;i++){
			for(int j=0;j<field.length;j++){
				if(field[i][j]!= BASIN_FIELD){
					field[i][j]=GREEN_FIELD;
				}
			}
		}
	}
}
